package com.gxuwz.check.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ActionInterceptor implements Interceptor {
    @Override
    public void destroy() {
    }

    @Override
    public void init() {
    }

    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        /**
         * 配置跨域请求
         */
        HttpServletResponse response = ServletActionContext.getResponse();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Request-Method", "POST,GET,PUT,DELETE");
        response.addHeader("Access-Control-Allow-Headers", "*");
        response.setCharacterEncoding("utf-8");


        /**
         * 判断发送的请求是否是OPTIONS,如果是，直接返回空，不让它进入,(这是由于请求是复杂请求)
         */
        HttpServletRequest request = ServletActionContext.getRequest();
        if (request.getMethod().equals("OPTIONS")){
            return null;
        }

        actionInvocation.invoke();
        return null;
    }
}
